<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <DeptTree
          :node_data="company"
          :is_Root="true"
          @addDepartment="addDepartment"
        />
        <hr />
        <el-tree
          :expand-on-click-node="false"
          :data="depts"
          :props="{ label: 'name' }"
          default-expand-all
        >
          <template #default="{ data }">
            <DeptTree
              @addDepartment="addDepartment"
              :node_data="data"
              @delDepartment="getDepartments"
              @editDepartment="editDepartment"
            />
          </template>
        </el-tree>
      </el-card>
      <AddDept
        ref="addDept"
        @addDepartment="getDepartments"
        :show-dialog.sync="showDialog"
        :nodedata="nodeData"
      ></AddDept>
    </div>
  </div>
</template>

<script>
import DeptTree from "./components/Dept-tree.vue";
import AddDept from "./components/add-dept.vue";
import { getDepartments } from "../../api/departments";
import { listToTreeData } from "../../utils/index";
export default {
  components: { DeptTree, AddDept },
  data() {
    return {
      company: {
        name: "江苏传智播客教育科技股份有限公司",
        manager: "负责人",
        id: "",
      },
      depts: [],
      showDialog: false,
      nodeData: {},
    };
  },

  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments();
      this.depts = listToTreeData(depts, "");
    },
    addDepartment(node_data) {
      this.showDialog = true;
      this.nodeData = node_data;
    },
    async editDepartment(node_data) {
      await this.$refs.addDept.getDepartmentDetail(node_data.id);
      this.showDialog = true;
      this.nodeData = node_data;
    },
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>